package com.macro.mall.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.BmsRecruitmentInformationMapper;
import com.macro.mall.mapper.BmsVolunteerRegistrationMapper;
import com.macro.mall.model.BmsRecruitmentInformation;
import com.macro.mall.model.BmsVolunteerRegistration;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.dto.BmsVolunteerRegistrationParam;
import com.macro.mall.portal.service.BmsVolunteerRegistrationService;
import com.macro.mall.portal.util.MemberUtil;
import com.macro.mall.portal.util.MemberUtils;
import lombok.Synchronized;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * 观光车点位图片Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class BmsVolunteerRegistrationServiceImpl implements BmsVolunteerRegistrationService {

    @Autowired
    private BmsVolunteerRegistrationMapper bmsVolunteerRegistrationMapper;

    @Autowired
    private BmsRecruitmentInformationMapper bmsRecruitmentInformationMapper;

    @Transactional(rollbackForClassName = {})
    @Override
    @Synchronized
    public int create(BmsVolunteerRegistration bmsVolunteerRegistration) throws Exception {
        UmsMember umsMember = MemberUtils.getUser();
        //根据token查询回来的数据进行赋值
        //获取用户名称
        if (null == bmsVolunteerRegistration.getUserName() || "".equals(bmsVolunteerRegistration.getUserName())) {
            bmsVolunteerRegistration.setUserName(umsMember.getUsername());
        }
        if (null == bmsVolunteerRegistration.getUserPhone() || "".equals(bmsVolunteerRegistration.getUserPhone())) {
            //获取用户手机号
            bmsVolunteerRegistration.setUserPhone(umsMember.getPhone());
        }
        bmsVolunteerRegistration.setStatus(0);
        bmsVolunteerRegistration.setApplicationTime(new Date());
        bmsVolunteerRegistration.setCreateUser(umsMember.getId());
        bmsVolunteerRegistration.setCreateTime(new Date());
        bmsVolunteerRegistration.setUpdateUser(umsMember.getId());
        bmsVolunteerRegistration.setUpdateTime(new Date());
        bmsVolunteerRegistration.setUserId(umsMember.getId());
        //查询报名信息
        int result = bmsVolunteerRegistrationMapper.selectBaoming(bmsVolunteerRegistration);
        if (result > 0) {
            throw new Exception("已报名，不可重复报名！");
        }
        BmsRecruitmentInformation bri = bmsRecruitmentInformationMapper.selectById(bmsVolunteerRegistration.getRecruitmentInformationId());
        //获取可报名人数和已报名人数
        if ((bri.getPeopleNumber() - bri.getIndividualsPeopleNumber() + 1) < 0) {
            throw new Exception("报名人数已满不可报名！");
        } else {
            bri.setIndividualsPeopleNumber((bri.getIndividualsPeopleNumber() + 1));
            bmsRecruitmentInformationMapper.update(bri);
        }
        result = bmsVolunteerRegistrationMapper.insert(bmsVolunteerRegistration);
        return result;
    }

    @Transactional(rollbackForClassName = {})
    @Override
    @Synchronized
    public int update(BmsVolunteerRegistration bmsVolunteerRegistration) {
        UmsMember umsMember = MemberUtils.getUser();
        bmsVolunteerRegistration.setStatus(3);
        bmsVolunteerRegistration.setUpdateUser(umsMember.getId());
        bmsVolunteerRegistration.setUpdateTime(new Date());
        bmsVolunteerRegistration.setUserId(umsMember.getId());
        BmsRecruitmentInformation bri = bmsRecruitmentInformationMapper.selectById(bmsVolunteerRegistration.getRecruitmentInformationId());
        bri.setIndividualsPeopleNumber(bri.getIndividualsPeopleNumber() - 1);
        bmsRecruitmentInformationMapper.update(bri);
        int result =bmsVolunteerRegistrationMapper.updateNew(bmsVolunteerRegistration);
        return result;
    }

    @Override
    public List<BmsVolunteerRegistration> getListByUserId(BmsVolunteerRegistration bmsVolunteerRegistration){
        UmsMember umsMember = MemberUtils.getUser();
        bmsVolunteerRegistration.setUserId(umsMember.getId());
        return bmsVolunteerRegistrationMapper.getListByUserId(bmsVolunteerRegistration);
    }

}
